package org.chaiyi.template.core.register.contractor.bean.process;

import lombok.extern.slf4j.Slf4j;
import org.chaiyi.template.core.common.common.BeanNameManage;
import org.chaiyi.template.core.common.contract.ContractInfo;
import org.chaiyi.template.core.framework.mongo.Repository;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;

/**
 * 自动添加Repository的bean定义
 *
 * @author Gong.Yang
 */
@Slf4j
public class MongoRepositoryRegister implements IServiceProcess {
    @Override
    public void register(ContractInfo<?, ?> contractInfo, BeanDefinitionRegistry beanDefinitionRegistry) {
        Class<?> entity = contractInfo.getEntity();
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Repository.class)
                .addConstructorArgValue(contractInfo.getSupporter())
                .addConstructorArgValue(entity)
                .getBeanDefinition();
        String beanName = BeanNameManage.getRepositoryName(entity);
        beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition);
    }
}
